package com.ifcreate.infowell.sys.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.json.JSONUtil;
import com.ifcreate.infowell.common.annotation.CNotNull;
import com.ifcreate.infowell.common.util.JsonResult;
import com.ifcreate.infowell.common.util.ValidatorUtils;
import com.ifcreate.infowell.entity.InfoWellNode;
import com.ifcreate.infowell.sys.service.FileUploadService;
import com.ifcreate.infowell.sys.service.SysInfoWellService;
import com.ifcreate.infowell.vo.NodeSearchVo;
import com.ifcreate.infowell.vo.RelationVo;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Map;

/**
 * @Author chenzedeng
 * @Email yustart@foxmail.com
 * @Create 2020-10-25 5:12 下午
 */
@RestController
@RequestMapping("/info-wall/api/sys/data")
public class SysNodeManageController {

    @Autowired
    private SysInfoWellService wellService;

    @Autowired
    private FileUploadService uploadService;

    /**
     * 创建或者更新新节点
     *
     * @param params
     * @return
     */
    @PostMapping("/addOrUpdateNode")
    public JsonResult addNode(@RequestBody Map<String, Object> params) {
        String time = MapUtils.getString(params, "time");
        if (StringUtils.isNotBlank(time)) {
            params.put("time", DateUtil.parse(time, "yyyy-MM-dd").getTime());
        }
        List<String> images = (List<String>) MapUtils.getObject(params, "images");
        if (CollectionUtil.isNotEmpty(images)) {
            params.put("images", JSONUtil.toJsonStr(images));
        }
        InfoWellNode wellNode = BeanUtil.mapToBean(params, InfoWellNode.class, true);
        ValidatorUtils.validateEntity(wellNode);
        return wellService.addOrUpdate(wellNode);
    }


    /**
     * 删除节点
     *
     * @param ids
     * @return
     */
    @PostMapping("/deleteNode")
    public JsonResult deleteNode(@CNotNull @RequestBody List<Long> ids) {
        return wellService.deleteNode(ids);
    }

    /**
     * 添加关系
     *
     * @param vo
     * @return
     */
    @PostMapping("/addRelation")
    public JsonResult addRelation(@RequestBody List<RelationVo> vo) {
        for (RelationVo v : vo) {
            ValidatorUtils.validateEntity(v);
        }
        return wellService.addRelation(vo);
    }


    /**
     * 删除关系 根据ID
     *
     * @param ids
     * @return
     */
    @PostMapping("/deleteRelationById")
    public JsonResult deleteRelationById(@CNotNull @RequestBody List<Long> ids) {
        return wellService.deleteRelationById(ids);
    }

    /**
     * 查询Node节点
     *
     * @param nodeSearchVo
     * @return
     */
    @PostMapping("/nodePageList")
    public JsonResult nodePageList(@RequestBody NodeSearchVo nodeSearchVo) {
        ValidatorUtils.validateEntity(nodeSearchVo);
        return wellService.nodePageList(nodeSearchVo);
    }

    /**
     * 查询获取这个节点的 第一级关联的子节点
     *
     * @param id
     * @return
     */
    @RequestMapping("/getNodeRelationByNodeId")
    public JsonResult getNodeRelationByNodeId(@CNotNull Long id) {
        return wellService.getNodeRelationByNodeId(id);
    }


    /**
     * 查询获取所有的关系
     *
     * @param vo
     * @return
     */
    @PostMapping("/getRelationList")
    public JsonResult getRelationList(@RequestBody NodeSearchVo vo) {
        return wellService.getRelationList(vo);
    }

    /**
     * 图片上传
     *
     * @param file
     * @return
     */
    @PostMapping("/uploadFile")
    public JsonResult uploadFile(@CNotNull MultipartFile file) {
        if (file.isEmpty()) {
            return JsonResult.errorForEmpty();
        }
        return uploadService.upload(file, "IMG");
    }

}
